package com.lysj.merchant.customer.controller;

import com.lysj.admin.common.merchant.domain.ComDeskVo;
import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.spring.foundation.base.BaseContent;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.merchant.desk.domain.Desk;
import com.lysj.merchant.desk.feign.OrderFeign;
import com.lysj.merchant.desk.service.DeskService;
import com.lysj.merchant.management.service.ManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Created by wtl on 2019-03-28 17:27
 * @description 小程序app
 */
@RestController
@RequestMapping("/merchant/management/customer")
@Slf4j
public class CustomerManagementController extends BaseContent {


    @Resource
    private ManagementService managementService;

    @Resource
    private OrderFeign orderFeign;

    /**
     * @author Created by wtl on 2019/3/28 17:48
     * @Description 商户获取验证码
     */
    @GetMapping("/get_captcha")
    public Resp getCaptcha(String phone) {
        return new Resp().success(managementService.getCaptcha(phone));
    }

    /**
     * @author Created by wtl on 2019/3/28 17:29
     * @Description 商户忘记密码
     */
    @PostMapping("/forget_password")
    public Resp forgetPassword(String phone, String captcha, String password) {
        return new Resp().success(managementService.forgetPassword(phone, captcha, password));
    }


    /*
     * @author drj
     * @date 2019-04-08 11:09
     * @Description :查询餐桌名称
     */
    @GetMapping("/find_desk_name")
    public Resp findDeskName(String deskId) {
        return new Resp().success(managementService.findDeskName(deskId), "");
    }

    /*
     * @author drj
     * @date 2019-04-08 11:11
     * @Description :查询门店信息
     */
    @GetMapping("/find_store_info")
    public Resp findStoreInfo(String storeId) {
        return new Resp().success(managementService.findStoreInfo(storeId));
    }


    /*
     * @author drj
     * @date 2019-04-09 16:48
     * @Description :查看该用户是否重复扫码
     */
    @GetMapping("/judge_repeat_sweep_code")
    public Resp judgeRepeatSweepCode(String customerId, String deskId) {
        log.info("customerId{}------------" + customerId);
        log.info("deskId{}------------" + deskId);
        //查询用户正在就餐的餐桌
        Desk cusDesk = managementService.deskService.getRepository().findByCustomerIdAndDelFlag(customerId, CommonConstant.NORMAL_FLAG);

        //若正在就餐的餐桌为空,查询传入deskId的餐桌状态
        if (ParamUtil.isBlank(cusDesk)) {
            Desk queryDesk = managementService.deskService.findOne(deskId);
            if (Desk.Status.FREE.getCode().equals(queryDesk.getStatus())) {
                return new Resp().success("1"); //什么东西都没干,该用户第一次扫码
            }
            return new Resp().success("2"); //其他用户扫了别人的餐桌
        }
        //若有就餐,餐桌id进行对比,查询该用户是否扫了其他餐桌
        if (!cusDesk.getId().equals(deskId)) {
            return new Resp().success("3");//当前用户有正在就餐的餐桌,但是扫了其他餐桌
        }
        //查询该用户是否有正在就餐的订单信息
        String orderId = orderFeign.queryCusPlaceOrderId(customerId);
        if (ParamUtil.isBlank(orderId)) {
            return new Resp().success(cusDesk.getCusNumber(), "4"); //当前用户有正在就餐的餐桌,尚未下单
        }
        return new Resp().success(orderId, "5"); //有下单,返回订单id
    }

    /*
     * @author drj
     * @date 2019-04-11 11:16
     * @Description :开桌
     */
    @PostMapping("/open_table")
    public Resp openTable(Desk model) {
        Desk desk = managementService.deskService.findOne(model.getId());
        desk.setCustomerId(model.getCustomerId());
        desk.setCusNumber(model.getCusNumber());
        desk.setStatus(Desk.Status.LOCK.getCode());
        managementService.deskService.save(desk);
        return new Resp().success("开桌成功");
    }

}
